package com.infoland.afanmall.model.order.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.ToastUtils;
import com.infoland.afanmall.R;
import com.infoland.afanmall.base.util.CONST;
import com.infoland.afanmall.base.util.LogKit;
import com.infoland.afanmall.base.util.ToolsKit;
import com.infoland.afanmall.base.util.UserKit;
import com.infoland.afanmall.base.util.okhttp.OkHttpKit;
import com.infoland.afanmall.model.order.adapter.OrderInfoAdapter;
import com.infoland.afanmall.model.order.entity.OrderInFoResponse;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * 订单详情界面
 */
public class OrderInfoActivity extends AppCompatActivity {

    @BindView(R.id.order_info_back)
    AppCompatImageView orderInfoBack;
    @BindView(R.id.order_info_title)
    TextView orderInfoTitle;
    @BindView(R.id.order_info_ok)
    TextView orderInfoOk;
    @BindView(R.id.cart_ll1)
    LinearLayout cartLl1;
    @BindView(R.id.pay_success_title)
    LinearLayout paySuccessTitle;

    @BindView(R.id.order_info_block1_img)
    AppCompatImageView orderInfoBlock1Img;
    @BindView(R.id.order_info_block1_text1)
    AppCompatTextView orderInfoBlock1Text1;
    @BindView(R.id.order_info_block1_text2)
    AppCompatTextView orderInfoBlock1Text2;
    @BindView(R.id.order_info_block1_text3)
    AppCompatTextView orderInfoBlock1Text3;
    @BindView(R.id.order_info_block1_ll)
    LinearLayout orderInfoBlock1Ll;
    @BindView(R.id.order_info_block1_img_you)
    AppCompatImageView order_info_block1_img_you;
    @BindView(R.id.order_info_block1)
    LinearLayoutCompat orderInfoBlock1;


    @BindView(R.id.order_info_block5_txt1_2)
    TextView orderInfoBlock5Txt12;
    @BindView(R.id.order_info_block5_txt1_3)
    TextView orderInfoBlock5Txt13;
    @BindView(R.id.order_info_block5_txt2_2)
    TextView orderInfoBlock5Txt22;
    @BindView(R.id.order_info_block5_txt3_2)
    TextView orderInfoBlock5Txt32;
    @BindView(R.id.order_info_block5_txt4_2)
    TextView orderInfoBlock5Txt42;
    @BindView(R.id.order_info_block5_txt5_2)
    TextView orderInfoBlock5Txt52;
    @BindView(R.id.order_info_block5_txt5_3)
    TextView orderInfoBlock5Txt53;
    @BindView(R.id.order_info_block5_txt6_2)
    TextView orderInfoBlock5Txt62;

    @OnClick({R.id.order_info_back, R.id.order_info_ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.order_info_back:
                finish();
                break;
            case R.id.order_info_ok:
                if ("完成".equals(orderInfoOk.getText().toString())) {
                    finish();
                }
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.order_info_activity);
        ButterKnife.bind(this);
        initData();
    }

    /*** Main**/
    private void initData() {
        Bundle bundle = this.getIntent().getExtras();
        if (bundle != null) {
            int orderId = bundle.getInt("orderId");
            int orderItemId = bundle.getInt("orderItemId");
            getOrderInFo(orderId, orderItemId);
        } else {
            ToastUtils.showShort("下单数据异常，请重新下单");
            finish();
        }
    }

    /*** 获取订单详情**/
    private void getOrderInFo(int orderId, int orderItemId) {
        JSONObject para = new JSONObject();
        para.put("orderId", orderId);
//        para.put("orderItemId", orderItemId);

        new OkHttpKit().send(
                response200 -> {
                    OrderInFoResponse orderInFoResponse = JSONObject.parseObject(response200, OrderInFoResponse.class);
                    updateUi(orderInFoResponse);
                },
                error401 -> {
                    LogKit.E("success401 ---->   ", "  " + error401);
                },
                error -> {
                    LogKit.E("error ---->   ", "  " + error);
                }
        ).post(CONST.ORDER_INFO + UserKit.getAccessToken(), para, this);
    }

    /*** 更新信息--根据订单状态（0.1.2.3.4）**/
    private void updateUi(OrderInFoResponse orderInFoResponse) {
        int o_status = orderInFoResponse.getO_status();
        int o_pay_status = orderInFoResponse.getO_pay_status();
        /*** 改变block1背景色，设置Title**/
        if (o_status == 1) {
            if (o_pay_status == 0) {
                order_info_block1_img_you.setVisibility(View.GONE);
                ToolsKit.setImgView(R.mipmap.dzfb, orderInfoBlock1Img);
                orderInfoBlock1.setBackgroundResource(R.color.order_red);//       红色
                orderInfoTitle.setText("等待支付");
                orderInfoOk.setText("");
                orderInfoBlock1Text1.setText("等待支付");
                orderInfoBlock1Text2.setText("剩余: ");
                ToolsKit.setTextColor(orderInfoBlock1Text2, R.color.order_wait_pay_write);
                orderInfoBlock1Text3.setText("需付款: ￥" + orderInFoResponse.getO_total_amount());
                ToolsKit.setTextColor(orderInfoBlock1Text3, R.color.order_wait_pay);
            } else {
                orderInfoTitle.setText("订单详情");
                orderInfoOk.setText("完成");
                orderInfoBlock1.setBackgroundResource(R.color.order_hui);//                灰色
            }
        } else {
            orderInfoTitle.setText("订单详情");
            orderInfoOk.setText("完成");
            orderInfoBlock1.setBackgroundResource(R.color.order_hui);//            灰色
        }

        /*** 摆放数据 block2Address**/
        setBlock2Address(orderInFoResponse);
        /*** 摆放数据 recycle Adapter**/
        setRecycle(orderInFoResponse);
        /*** 摆放数据 block4 **/
        setBlock4(orderInFoResponse);
        /*** 摆放数据 block5**/
        setBlock5(orderInFoResponse);

    }

    private void setBlock5(OrderInFoResponse orderInFoResponse) {
//       订单编号
        orderInfoBlock5Txt12.setText("" + orderInFoResponse.getMain_code() + "");
        orderInfoBlock5Txt13.setOnClickListener(v -> {
            ToolsKit.setSleep(orderInfoBlock5Txt13, 2000);
            boolean b = ToolsKit.copyText(orderInfoBlock5Txt12.getText().toString());
            if (b) {
                ToastUtils.showShort("复制完成");
            } else {
                ToastUtils.showShort("复制失败");
            }
        });
        //       下单时间
        orderInfoBlock5Txt22.setText("" + orderInFoResponse.getCreate_time() + "");
        //        支付方式
        Object payment_type_v = orderInFoResponse.getPayment_type_v();

        if (payment_type_v == null) {
            payment_type_v = "无";
        }
        orderInfoBlock5Txt32.setText("" + payment_type_v + "");
        //        支付时间
        Object payment_time = orderInFoResponse.getPayment_time();
        if (payment_time == null) {
            payment_time = "无";
        }
        orderInfoBlock5Txt42.setText("" + payment_time + "");
        //        交易单号
        orderInfoBlock5Txt52.setText("" + "---- 暂时没有 交易单号  PaymentCode---" + "");
        orderInfoBlock5Txt53.setOnClickListener(v -> {
            ToolsKit.setSleep(orderInfoBlock5Txt13, 2000);
            boolean b = ToolsKit.copyText(orderInfoBlock5Txt52.getText().toString());
            if (b) {
                ToastUtils.showShort("复制完成");
            } else {
                ToastUtils.showShort("复制失败");
            }
        });
        //        配送方式
        orderInfoBlock5Txt62.setText("普通快递" + "");
    }

    @BindView(R.id.order_info_block4_count)
    TextView orderInfoBlock4Count;
    @BindView(R.id.order_info_block4_price)
    TextView orderInfoBlock4Price;
    @BindView(R.id.order_info_block4_you_fei)
    TextView orderInfoBlock4YouFei;

    private void setBlock4(OrderInFoResponse orderInFoResponse) {
        orderInfoBlock4Count.setText(orderInFoResponse.getGoods_count() + "件");
        orderInfoBlock4Price.setText("￥" + orderInFoResponse.getO_total_amount() + "");
        orderInfoBlock4YouFei.setText("免邮");
    }

    OrderInfoAdapter orderInfoAdapter;
    @BindView(R.id.order_info_recy)
    RecyclerView orderInfoRecy;

    private void setRecycle(OrderInFoResponse orderInFoResponse) {
        orderInfoAdapter = new OrderInfoAdapter(R.layout.order_info_include_item, orderInFoResponse.getOrderItemList());
        orderInfoRecy.setLayoutManager(new LinearLayoutManager(this));
        orderInfoRecy.setAdapter(orderInfoAdapter);
    }

    @BindView(R.id.order_info_block2_img)
    AppCompatImageView orderInfoBlock2Img;
    @BindView(R.id.order_info_block2_name)
    AppCompatTextView orderInfoBlock2Name;
    @BindView(R.id.order_info_block2_phone)
    AppCompatTextView orderInfoBlock2Phone;
    @BindView(R.id.order_info_block2_address)
    AppCompatTextView orderInfoBlock2Address;
    @BindView(R.id.order_info_block2_ll)
    LinearLayout orderInfoBlock2Ll;
    @BindView(R.id.order_info_block2)
    LinearLayoutCompat orderInfoBlock2;

    private void setBlock2Address(OrderInFoResponse orderInFoResponse) {
        OrderInFoResponse.OrderShipmentBean orderShipment = orderInFoResponse.getOrderShipment();
        orderInfoBlock2Address.setText(orderShipment.getReceiver_address() + "");
        orderInfoBlock2Name.setText(orderShipment.getReceiver_name() + "");
        orderInfoBlock2Phone.setText(ToolsKit.phoneXing(orderShipment.getReceiver_tel() + ""));
    }


}
